package com.taotao.portal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.portal.pojo.CartItem;
import com.taotao.portal.service.CartService;

@Controller
public class CartController {
	@Autowired
	private CartService cartService;
	@RequestMapping(value="/cart/cart")
	public String showCart(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<CartItem> cartItemList = cartService.getCartItemList(request, response);
		model.addAttribute("cartList", cartItemList);
		return "cart";
	}
	
	@RequestMapping(value="/cart/add/{id}", produces="text/html;charset=utf-8")
	public String addItem(HttpServletRequest request, HttpServletResponse response, @PathVariable long id, @RequestParam(defaultValue="1") Integer num) {
		cartService.add(request, response, id, num);
		return "redirect:/cart/cartSuccess.html";
	}
	@RequestMapping(value="/cart/cartSuccess")
	public String showSuccess() {
		return "cartSuccess";
	}
	
	@RequestMapping(value="/cart/deleSuccess")
	public String showDeleSuccess() {
		return "deleSuccess";
	}
	@RequestMapping(value="/cart/update/num/{id}/{num}")
	@ResponseBody
	public TaotaoResult update(@PathVariable long id, @PathVariable Integer num, HttpServletRequest request, HttpServletResponse response) {
		return cartService.updateNum(id, num, request, response);
	}
	@RequestMapping(value="/cart/delete/{id}")
	public String delete(@PathVariable long id, HttpServletRequest request, HttpServletResponse response) {
		cartService.delete(id, request, response);
		return "redirect:/cart/deleSuccess.html";
	}
	
}
